package com.ayit.simpleklog;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSONObject;
import com.ayit.klog.DateTimeUtil;
import com.ayit.klog.KLog;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import java.util.Locale;


public class MainActivity extends AppCompatActivity {
    public static DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.CHINA);
    public static DateFormat dataFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
    int count = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        File tagFileParent = new File("/sdcard/klog");
        if (!tagFileParent.exists()) {
            tagFileParent.mkdirs();
        }
        List<File> fileList = Arrays.asList(tagFileParent.listFiles());
        Collections.sort(fileList, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                return (int) (o2.lastModified() - o1.lastModified());
            }
        });
        for (File file : fileList) {
                Log.d("xxxxx","删除："+file.getName()+" del=");
        }
        KLog.init(this,true,true,"KYLog","/sdcard/klog",3,1024*1024*2);
//        outLog();
//        outLog();
//        outLog();
//        outLog();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//
//                while (true){
//                    SystemClock.sleep(20);
//                    JSONObject obj = new JSONObject();
//                    obj.put("name","张三");
//                    obj.put("age","18");
//                    obj.put("sex","男");
//                    obj.put("time",System.currentTimeMillis());
//
//                    KLog.json(obj.toJSONString());
//                }
//            }
//        }).start();
        KLog.d("打印信息TAG","打印信息T");
        KLog.d("打印信息","123");
//        try{
//            int a = 1/0;
//        }catch (Exception e){
//            KLog.e("异常信息打印",e);
//        }
        
//        //检查权限（NEED_PERMISSION）是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
//        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
//                != PackageManager.PERMISSION_GRANTED) {
//            //用户已经拒绝过一次，再次弹出权限申请对话框需要给用户一个解释
//            if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission
//                    .WRITE_EXTERNAL_STORAGE)) {
//                Toast.makeText(this, "请开通相关权限，否则无法正常使用本应用！", Toast.LENGTH_SHORT).show();
//            }
//                Toast.makeText(this, "申请权限", Toast.LENGTH_SHORT).show();
//            //申请权限
//            Log.d("KLog","111");
//            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.CAMERA}, 1);
//            Log.d("KLog","111111");
//        } else {
//            Log.d("KLog","222");
//            Toast.makeText(this, "授权成功！", Toast.LENGTH_SHORT).show();
//            KLog.init(this,true);
//            KLog.d("123");
//            Log.d("KLog","xxxx");
//        }
    }
    
    private void outLog(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                
                while (true){
//                    SystemClock.sleep(20);
                    JSONObject obj = new JSONObject();
                    obj.put("name","张三");
                    obj.put("age","18");
                    obj.put("sex","男");
                    obj.put("time",System.currentTimeMillis());
//
////                    KLog.json(obj.toJSONString());
//                    KLog.d(obj.toJSONString());
//                    KLog.e();
                    int i = getIdx();
                    KLog.d("data:"+i+"XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX,tid="+Thread.currentThread().getId());
                    KLog.json(obj.toJSONString());
                    if (i > 20000) {
                        android.os.Process.killProcess(android.os.Process.myPid());
                    }
                }
            }
        }).start();
    }
    int i=0;
    private synchronized int getIdx(){
        i++;
        return i;
    }

    @Override
    protected void onResume() {
        super.onResume();
//        Log.d("KLog", "resume");
        KLog.i("resume");
    }
    

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        
    }
    
}
